<template>
  <div>
    <van-empty class="custom-image" :image="image" :description="msg" />
  </div>
</template>

<script>
  import queryString from 'query-string'
  import jwt_decode from 'jwt-decode'
  import {
    Empty,
    Toast
  } from 'vant'
  export default {
    name: "callback",
    components: {
      'van-empty': Empty
    },
    data: function () {
      return {
        msg: '正在加载用户信息...',
        image: "./static/imgs/loading.png"
      }
    },
    methods: {
      //加载用户信息
      async loadUserInfo() {
        //获取请求地址的token信息
        var resToken = this.$route.hash.substring(2, this.$route.hash.length);
        //格式化请求地址的参数信息
        var callback = queryString.parse(resToken)
        //获取认证的token
        var id_token = jwt_decode(callback.id_token)
        //存储认证token
        this.$cookies.set('access_token', callback.access_token, 60 * 45); //存储50分钟
        try {
          //根据id获取用户基本信息
          var res = await this.$ajax.getByToken(`/api/user/info/${id_token.sub}`, callback.access_token)
          if (res.status == 0) {
            this.$user.Add(res.data);
            //跳转页面
            this.$router.replace('/task');
          } else {
            Toast.fail(res.msg)
            return;
          }
        } catch (error) {
          Toast.fail(error)
          return;
        }
      }
    },
    async mounted() {
      await this.loadUserInfo();
      //显示底部栏
      this.$store.commit('updateShowFoot', false)
    }
  }

</script>
